//
//  ChatViewCell.m
//  EaseMob
//
//  Created by wwj on 16/5/31.
//  Copyright © 2016年 wfw. All rights reserved.
//

#import "ChatViewCell.h"
#import "UIImage+Extend.h"
#import "EMTextMessageBody.h"
#import "ChatVoiceView.h"
#import "ChatImageView.h"

@interface ChatViewCell ()
@property (nonatomic, weak) UILabel *timeView;
@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UIButton *textView;
@property (weak, nonatomic) ChatVoiceView *voiceView;
@property (weak, nonatomic) UIImageView *bgImageView;
@property (weak, nonatomic) ChatImageView *chatImageView;
@end

@implementation ChatViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    
    UIImageView *bgImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:bgImageView];
    self.bgImageView = bgImageView;
    
    //1:时间
    UILabel *timeView=[[UILabel alloc]init];
    timeView.textColor=[UIColor blackColor];
    timeView.textAlignment=NSTextAlignmentCenter;
    timeView.font=kTimeFont;
    [self.contentView addSubview:timeView];
    self.timeView=timeView;
    //2:头像
    UIImageView *iconView=[[UIImageView alloc]init];
    [self.contentView addSubview:iconView];
    self.iconView=iconView;
    //3: 正文
    UIButton *textView=[[UIButton alloc]init];
    [textView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //自动换行
    textView.titleLabel.numberOfLines=0;
    textView.titleLabel.font=kTextFont;
    //设置内边距
    textView.contentEdgeInsets=UIEdgeInsetsMake(kBtnPading*0.5, kBtnPading*0.5, kBtnPading*0.5, kBtnPading*0.5);
    [self.contentView addSubview:textView];
    self.textView=textView;
    
    //设置语音
    ChatVoiceView *voiceView = [[ChatVoiceView alloc] init];
    [self.contentView addSubview:voiceView];
    self.voiceView = voiceView;
    
    //设置图片
    ChatImageView *chatImageView = [[ChatImageView alloc] init];
    [self.contentView addSubview:chatImageView];
    self.chatImageView = chatImageView;
    
    
    //设置cell的背景颜色
    [self setBackgroundColor:[UIColor clearColor]];
    
    self.backgroundColor = [UIColor clearColor];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.iconView.layer.cornerRadius = self.iconView.width * 0.5;
}

- (void)setFrameModel:(EMMessageFrame *)frameModel{
    _frameModel = frameModel;
    EMMessage *message = frameModel.message;
     EMMessageBody *msgBody = message.body;
    //1:时间
    self.timeView.text = _frameModel.time;
    self.timeView.frame = _frameModel.timeF;
    //2:头像
    NSString *icon=(_frameModel.type==MessageTypeMe)?@"me":@"other";
    self.iconView.image=[UIImage imageNamed:icon];
    self.iconView.frame=_frameModel.iconF;
    
    if (_frameModel.type==MessageTypeMe) {
        self.bgImageView.image = [UIImage resizedImage:@"chat_send_nor"];
    }else{
        self.bgImageView.image = [UIImage resizedImage:@"chat_recive_nor"];
    }
    
    switch (msgBody.type) {
        case EMMessageBodyTypeText://文本类型
            [self setupText:msgBody];
            break;
        case EMMessageBodyTypeImage://图片类型
            _textView.hidden = YES;
            _voiceView.hidden = YES;
            _chatImageView.hidden = NO;
            _chatImageView.frame = _frameModel.imageF;
            _chatImageView.frameModel = _frameModel;
            _bgImageView.frame = _frameModel.imageF;
            break;
            
        case EMMessageBodyTypeLocation://位置类型
            
            break;
            
        case EMMessageBodyTypeVoice://语音类型
            _textView.hidden = YES;
            _chatImageView.hidden = YES;
            _voiceView.hidden = NO;
            self.voiceView.frame = _frameModel.voiceF;
            self.voiceView.frameModel = _frameModel;
            _bgImageView.frame = _frameModel.voiceF;
            break;
            
        case EMMessageBodyTypeVideo://位置类型
            
            break;
            
        case EMMessageBodyTypeFile://文件类型
            
            break;
            
        case EMMessageBodyTypeCmd://命令类型
            break;
            
    }
   
}

- (void)setupText:(EMMessageBody *)msgBody{
    _textView.hidden = NO;
    _chatImageView.hidden = YES;
    _voiceView.hidden = YES;
    EMTextMessageBody *textBody = (EMTextMessageBody *)msgBody;
    //3: 正文
    [self.textView setTitle:textBody.text forState:UIControlStateNormal] ;
    self.textView.frame=_frameModel.textF;
    //3.1设置文字背景图片
    if (_frameModel.type==MessageTypeMe) {
        [self.textView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
      
    }else{
        [self.textView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    _bgImageView.frame = _frameModel.textF;
}

@end
